package gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import gwt.client.events.NavigationChangeEvent;
import gwt.client.interfaces.NavigationChangeListener;
import gwt.client.widgets.MenuBtnWidget;

import java.util.ArrayList;


/**
 * Created by Sam on 15/03/14
 * table based layout composite widget
 *
 * http://www.gwtproject.org/doc/latest/DevGuideUiPanels.html
 * "VerticalPanel can usually be replaced by a simple FlowPanel (since block-level elements will naturally stack up vertically)."
 * No longer required to implement HasClickHandlers
 */

public class NavigationPanel extends Composite {

    private ArrayList<NavigationChangeListener> listeners;
    FlowPanel verticalPanel;

    public NavigationPanel() {

        super();

        verticalPanel = new FlowPanel();

        // widget that acts as the parent (Composite's container)
        // for this Composite pg 120 & 121

        initWidget(verticalPanel);

        listeners = new ArrayList<NavigationChangeListener>();

        createButton("Repositories", "yellow", "repository.png");
        createButton("Download", "turquoise", "download.png");
        createButton("Upload", "green", "upload.png");
        createButton("Settings", "red", "settings.png");
        createButton("Help", "orange", "help.png");
        createButton("Reporting", "pink", "reporting.png");

        verticalPanel.getElement().getStyle().setWidth(200, Style.Unit.PX); // same width as mainPanel.addWest() 200

        // Not handling events internal to this Widget,
        // instead handle Events external by delegating to Widget.
        // verticalPanel.sinkEvents(Event.ONCLICK);
    }

    private void createButton(String text, String colour, String img) {

        MenuBtnWidget menuBtnWidget = new MenuBtnWidget(text, colour, img);

        menuBtnWidget.addMouseOverHandler(new MouseOverHandler() {
            @Override
            public void onMouseOver(MouseOverEvent event) {
                GWT.log("NavigationPanel mouseOver() " + event.toString());
            }
        });

        menuBtnWidget.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                MenuBtnWidget btn = ((MenuBtnWidget) event.getSource());
                fireNavigationChangedEvent(btn.name);
            }
        });

        verticalPanel.add(menuBtnWidget);
    }

    /**
     * Listener & Event methods
     */

    public void addNavigationChangeListener(NavigationChangeListener listener) {
        listeners.add(listener);
    }

    private void fireNavigationChangedEvent(String id) {

        NavigationChangeEvent event = null;

        for (NavigationChangeListener listener : listeners) {

            if (event == null) {
                event = new NavigationChangeEvent(this, id);
            }

            listener.onNavigationChangeEvent(event);
        }
    }

    /*
    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        switch(event.getTypeInt()) {
            case Event.ONCLICK:
                GWT.log("onBrowserEvent() " + event.getEventTarget());
        }
    }
    */

    /*
    @Override
    // really need button level info, not whole FlowPanel
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        //return addDomHandler(handler, ClickEvent.getType());
        return verticalPanel.addDomHandler(handler, ClickEvent.getType());
        //return verticalPanel.addHandler(handler, ClickEvent.getType());
    }
    */
}
